<?php

chdir(dirname(__FILE__)."/../..");
$path = getcwd();
chdir("../");
$path3 = getcwd();
chdir(dirname(__FILE__));
include_once $path3."/lib/core.class.php";
include_once $path3."/include/classes/salesman.php";


class ReSellersManagement extends IPlugin 
{
    var $color;
    
    function ReSellersManagement($parent = null)
    {
        IPlugin::IPlugin($parent);
        $this->tplfolder = dirname(__FILE__)."/tpl";
        $this->color = "darkgray";
    }
    
    function getPluginTag() { return "rmanagement"; }
    function getPluginName() { return "Resellers Class Plugin"; }
    function getPluginVersion() { return "1.0.0"; }
    
    function getProperty($p = null)
    {
        if(null == $p)
        {
            return array("MenuBlockTitle", "MenuTitle");
        }
        $result = "";
        switch ($p)
        {
            case "MenuBlockTitle": { return "Sales Management"; } break;
            case "MenuTitle" : { return "Sales person"; } break;
        };
        return $result;
    }

    // ----------------------------------------------------------------------------------
    // --- [ Handling all actions ] -----------------------------------------------------
    // ----------------------------------------------------------------------------------
    function doAction($params)
    {
        if(null != ($result = IPlugin::doAction($params)))
        {
            return $result;
        }
        
        $core = getCoreInstance();
        $arg = $core->getGET();
        $db = $core->getProperty("db");
        
        switch ($_POST['event'])
        {
            case "EditSeller":{
                                    
            }break;
       }
        
        switch ($_GET['action'])
        {
            case "doDeleteSeller" :{
                $id = $_GET['id'];
                 $referer = trim($_SERVER['HTTP_REFERER']);

                $s = new SalesMan($db);
                $s->delete($id);
                $s->finish();
                header('location: '.$referer);
                exit();
                    
            }break;
        }
        
        
        return true;
    }
    
    // ----------------------------------------------------------------------------------
    // --- [ Render pages ] -------------------------------------------------------------
    // ----------------------------------------------------------------------------------

    function Render($params)
    {
        global $_G;
        if(null != ($result = IPlugin::Render($params)))
        {
            return $result;
        }
        
        $core = getCoreInstance();
        $render = $core->framework->smarty;
        $render->assign("baseurl", "?".$this->getURL()."&page=".$_G["page"]);
        $db = $core->getProperty("db");

        $page = "";

        switch ($_G["page"]) {
            
            case "edit":
                $page = $this->renderSalersEditPage($core, $render, $db);
                break;

            default:
                $page = $this->renderSalersList($core, $render, $db);
                break;
        }

        return $render->fetch("file:".$this->tplfolder."/".$page);
    }

    // ----------------------------------------------------------------------------------
    // --- [ Render Clients List page ] -------------------------------------------------------------
    // ----------------------------------------------------------------------------------
    
    function renderSalersList(&$core, &$render, &$db)
    {
        $pg = &$core->getSnippet("paginator");

        $params = array(
                    "cb_class" => &$this,
                    "cb_func" => saler_actions_cb,
                    "urlbase" => $this->getURL(),
                    "caption" => "Salers list",
                    "emptymsg" => "<center>No registered salers yet</center>",
                    "header" => array("#", "First Name","Last Name", "Commission", "DateAdded", "Actions"),
                    "items" => "",
                    "source" => "sql", // -- sql, array
                    "sql_items" => "select * from resellers order by createdate desc",
                    "sql_count" => "select count(*) from resellers",
                    "ippg" => 20, // -- items per page
                    "nppv" => 5, // -- number pages per view
                    );

        $HTML = $pg->Render($params);
        $render->assign("clients", $HTML);
        //debug($_SESSION);
        return "rsales_list.tpl.php";
    }


    function saler_actions_cb($data)
    {
        $str_get = "";
        foreach ($_GET as $k => $v)
        {
            $str_get .= ($str_get == "" ? "" : "&") . "{$k}=" . str2db($v);
        }
     
        $dt = array(
                array($data["id"], ""),
                array($data["fname"], ""),
                array($data["lname"], ""),
                array($data["commission"], ""),
                array(date("m/d/Y", strtotime($data["createdate"])), ""),
                array("<a title='Edit' href=\"?" . $str_get . "&page=edit&id={$data['id']}\">edit</a>&nbsp;&nbsp;<a title='Delete' href=\"?" . $str_get . "&action=doDeleteSeller&id={$data['id']}\">delete</a>","align=\"center\""),
                );
        return $dt;
    }

    // ----------------------------------------------------------------------------------
    // --- [ Render Clients Edit page ] -------------------------------------------------------------
    // ----------------------------------------------------------------------------------
    
    function renderSalersEditPage(&$core, &$render, &$db)
    {
        
        return "rsales_edit.tpl.php";
    }

};


?>